#import "BWConfigPopUpButton.h"
#import "BWConfigTemplate.h"

@implementation BWConfigPopUpButton


- (id) initWithCoder: (NSCoder *) coder
{
    if (self = [super initWithCoder: coder]) {
        firstTimeAwaken = YES;
    }

    return (self);

} // initWithCoder


- (void) addItemTitle: (NSString *) title
               action: (SEL) action
              withTag: (int) tag
               toMenu: (NSMenu *) menu
{
    NSMenuItem *item;
    item = [[NSMenuItem alloc]
               initWithTitle: title
               action: action
               keyEquivalent: @""];

    [item setTag: tag];

    [item setTarget: self];
    [menu addItem: item];

} // addItemTitle


- (NSMenu *) makeMenu
{
    NSString *categoryName;
    categoryName = [delegate categoryNameForPopUpButton: self];

    NSMenu *menu = [[NSMenu alloc] init];
    
    templateNames = [[BWConfigTemplate namesForCategory: categoryName] copy];

    if ([templateNames count] > 0) {
        
        int i;
        for (i = 0; i < [templateNames count]; i++) {
            NSString *filename;
            filename = [templateNames objectAtIndex: i];

            [self addItemTitle: filename
                  action: @selector(chooseTemplate:)
                  withTag: i
                  toMenu: menu];
        }

        [menu addItem: [NSMenuItem separatorItem]];
    }

    [self addItemTitle: @"Save Settings To Template..."
          action: @selector(saveTemplate:)
          withTag: 0
          toMenu: menu];

    [self addItemTitle: @"Edit Template List..."
          action: @selector(editTemplateList:)
          withTag: 0
          toMenu: menu];

    return ([menu autorelease]);

} // makeMenu


- (void) updateMenu
{
    NSMenu *menu = [self makeMenu];
    [self setMenu: menu];
    
    // select the next-to-last item, which is the 'save settings to 
    // template' option
    [self selectItemAtIndex: [menu numberOfItems] - 2];

} // updateMenu


- (void) awakeFromNib
{
    if (firstTimeAwaken) {
        [self updateMenu];
    }

} // awakeFromNib


- (void) editTemplateList: (id) sender
{
} // editTemplateList


- (void) chooseTemplate: (id) sender
{
    int tag = [sender tag];

    NSString *filename;
    filename = [templateNames objectAtIndex: tag];

    NSString *categoryName = [delegate categoryNameForPopUpButton: self];

    BWConfigTemplate *template;
    template = [BWConfigTemplate loadTemplateForCategory: categoryName
                                 name: filename];

    [delegate useTemplate: template
              fromPopUpButton: self];

} // chooseTemplate


// --------------------------------------------------
// save sheet jazz


- (void) saveTemplateName: (NSString *) name
{
    NSString *categoryName = [delegate categoryNameForPopUpButton: self];

    // !!! make sure it's not already there
    BWConfigTemplate *template;
    template = [BWConfigTemplate newTemplateInCategory: categoryName
                                 name: name];

    [delegate populateTemplate: template
              forPopUpButton: self];

    [template save];
    [self updateMenu];

} // saveTemplateName


- (void) saveSheetDidEnd: (NSWindow *) sheet
              returnCode: (int) returnCode
             contextInfo: (void *) contextInfo
{
    if (returnCode == NSOKButton) {
        
        NSString *templateName = [saveSheetNameTextField stringValue];
        [self saveTemplateName: templateName];

    } else if (returnCode == NSCancelButton) {

    } else {

    }

    [sheet close];

} // saveSheetDidEnd


- (void) saveTemplate: (id) sender
{
    if (saveSheet == nil) {
        if (![NSBundle loadNibNamed: @"BWTemplateButtonSaveSheet"  owner: self]) {
            NSLog (@"could not load nib");
        }
    }

    NSWindow *window = [delegate windowForConfigSheetForPopUpButton: self];

    [saveSheetNameTextField setStringValue: @""];

    [NSApp beginSheet: saveSheet
           modalForWindow: window
           modalDelegate: self
           didEndSelector: @selector(saveSheetDidEnd:returnCode:contextInfo:)
           contextInfo: NULL];
    
} // saveTemplate


- (IBAction) saveSheetOKButton: (id) sender
{
    [NSApp endSheet: saveSheet  returnCode: NSOKButton];
} // saveSheetOKButton


- (IBAction) saveSheetCancelButton: (id) sender
{
    [NSApp endSheet: saveSheet  returnCode: NSCancelButton];
} // saveSheetCancelButton


@end // BWConfigPopUpButton

